/**
 * Created by zhangyi on 2017/11/17.
 */
mainApp.factory("attendanceFactory",function() {
    var service={};

    //修改时获取数据
    var object;
    service.setAttend=function(data){
        object=data;

    }
    service.getAttend=function(){
        return object;
    }

    //新增批量考勤的service
    service.addAttendData=function(attendObjs){
        console.log(attendObjs)
        RequestUtils.postJson("../am/add",attendObjs)
            .done(function(resp){
                console.log(resp);
                if(resp.resultCode==0){
                    CommonUtils.buildSuccessHint('add',"成功");
                }else{
                    CommonUtils.buildFailHint('add',"失败");
                }
            });
    };

    return service;
});

mainApp.controller("amListController",function($scope,$http,$state,$filter,attendanceFactory,empFactory,overallSituationService) {
    overallSituationService.loginIntercept();

    //今天日期
    $scope.today=$filter('date')(new Date(), 'yyyy-MM-dd');

    //全部人员，包括部门
    $scope.list=empFactory.selectAll();

    /*//查询工号，跳转修改页面
    $scope.selectAttendance=function(jobnum){
        //添加标签
        var tab={url:"attend-update",text:"部门详情",id:"attend-update-tab",closeable:1};
        $scope.$emit('addTab', tab);

        var attend=attendanceFactory.getAmByJobnum(jobnum);
        attendanceFactory.setAttend(attend);
        $state.go('attendance-update');
    }*/

    $scope.addAttend=function(attendObjs){
        console.log(attendObjs)
        var attendanceManagements=[];
        for(var i=0;i<attendObjs.length;i++){
            if(attendObjs[i].attendStatus=="" || attendObjs[i].attendStatus==null){
                attendObjs[i].attendStatus="0";
            }
            var attendObj={
                "employee.id":attendObjs[i].id,
                "date":$scope.today,
                "status":attendObjs[i].attendStatus
            };
            attendanceManagements.push(attendObj);
        }
        attendanceFactory.addAttendData(attendanceManagements);
    };
});